package com.rtp2p.rtcoresdkdemo.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation.findNavController

abstract class BaseFragment : Fragment() {
    protected abstract fun initWorkspaceAction(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): View

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initWorkspaceAction(inflater, container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setOnApplyWindowInsetsListener(view, null)
    }

    protected fun setOnApplyWindowInsetsListener(view: View, height: Int?) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            //v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            v.setPadding(
                systemBars.left,
                height ?: systemBars.top,
                systemBars.right,
                height ?: systemBars.bottom
            )
            insets
        }
    }

    protected fun setOnApplyWindowInsetsListenerBottom(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            //v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            v.setPadding(
                systemBars.left,
                systemBars.top,
                systemBars.right,
                0
            )
            insets
        }
    }

    // 隐藏状态栏
    protected fun hideStatusBar() {
        //activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // 全屏

        // 设置全屏
        activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
    }

    // 显示状态栏
    protected fun showStatusBar() {
        //activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // 清除全屏

        // 取消全屏
        activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
        activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

    protected fun startFragment(action: Int) {
        view?.let {
            // 获取NavController
            val navController = findNavController(it)
            // 导航到SecondFragment
            navController.navigate(action)
        }
    }

    protected fun finish() {
        if (view == null) return
        view?.let {
            // 获取 NavController 实例
            val navController = findNavController(it)
            // 调用 navigateUp() 方法返回上一个 Fragment
            val isSuccess = navController.navigateUp()

            //没有后退栈时直接关闭Activity
            if (!isSuccess) if (activity != null) activity?.finish()
        }
    }
}